<?php

namespace App\Repositories\Eloquent\Biz;

use Illuminate\Container\Container as App;
use Intervention\Image\Facades\Image;
use App\Repositories\Eloquent\Repository;

class GiftRepository extends Repository
{        
    /**
     * @param App
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Biz\Gift';
    }

  
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($order = '', $status = '')
    {
        $result = $this->model->with('exchanged')->withCount('exchanged')
                ->when($status, function($query, $status){
                    return $query->where('status', $status);
                })
                ->orderBy('id','desc');

        return $result;
    }

    /**
     * 查找指定gift
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function show($id)
    {   
        $gift = $this->find($id);      

        if (!$gift) 
            return $this->responseFail(404);

        if(empty($gift->image)) 
            $gift->image = '/assets/images/plus.fw.png';

        return $gift;
    }


    public function renew($request, $id)
    {   
        $gift = $this->find($id);

        $imgurl = $gift->image;

        if($request->hasFile('image')){

            $file = $request->file('image');

            $img      = Image::make($file);

            $width = $img->width();

            $height = $img->height();

            if( $width != $height)
                return $this->responseFail(405, '请选择正方形图片');

            $origina     = $file->getClientOriginalName();

            $extension   = $file->getClientOriginalExtension();

            $path        = "/assets/images/gifts/";

            $upload_path = public_path() . $path;

            if (!is_dir($upload_path)) {
                mkdir($upload_path, 0755, true);
            }

            $filename = time(). "." . $extension;

            $upfile   = $upload_path . $filename;

            $img->save($upfile);

            $imgurl   = $path . $filename;            
        }

        $result = $this->update([
            'name'      => $request->name,
            'price'     => $request->price,
            'image'     => $imgurl,
            'status'    => $request->status == 1?1:0,
        ], $id);

        if( !!$result )
            return $this->responseSuccess('修改礼品成功');
        
        return $this->responseFail(500, '修改礼品失败');
    }


    public function store($request)
    {
        $imgurl = '';

        if($request->hasFile('image')){
            $file = $request->file('image');

            $img      = Image::make($file);

            $width = $img->width();

            $height = $img->height();

            if( $width != $height)
                return $this->responseFail(405, '请选择正方形图片');

            $origina     = $file->getClientOriginalName();

            $extension   = $file->getClientOriginalExtension();

            $path        = "/assets/images/gifts/";

            $upload_path = public_path() . $path;

            if (!is_dir($upload_path)) {
                mkdir($upload_path, 0755, true);
            }

            $filename = time(). "." . $extension;

            $upfile   = $upload_path . $filename;

            $img->save($upfile);

            $imgurl   = $path . $filename;            
        }

        $result = $this->create([
            'name'      => $request->name,
            'price'     => $request->price,
            'image'     => $imgurl,
            'status'    => 1,
        ]);

        if( !!$result )
            return $this->responseSuccess('添加礼品成功', $result, route('point.gift.index'));
        
        return $this->responseFail(500, '添加礼品失败');
    }

    
}